/**
 * Created by gzf on 2015/10/26.
 */
user_config = {
    currPageNo:1,
    canNext:true,
    tradeType:{'deal':['收入','in-wallect-icon','+'],'refund':['退款','out-wallect-icon','-'],'surplus-rfd':['退款','out-wallect-icon','-'],'withdraw':['提现','out-wallect-icon','-'],'recharge':['充值','in-wallect-icon','+'],'expense':['支出','out-wallect-icon','-'],'surplus':['支出','out-wallect-icon','-'],'assurance':['支出','out-wallect-icon','-']}
}
function addBanlance(pageNo){
    console.log(pageNo);
    isLoading(true);
    $.getJSON('balance.json?pageNo='+pageNo,function(data){
        pageNo == 1 && dealBanHead(data.data.userinfo.profile.remain_money,data.data.income);
        dealBanData(data);
    });
}
function dealBanHead(balance,income){
    console.log('head data');
    $('#my-banlance').text('￥' + balance);
    $('#my-income').text('￥' + income);
};
function dealBanData(banJson){
    if(banJson.status){
        var html = '';
        for(var i= 0,tradeList=banJson.data.tradelist.data,len=tradeList.length;i<len;i++){
            var typeArr = user_config.tradeType[tradeList[i].trade_type];
            if(typeArr){
                html += '<li class="list-group-item">'
                    +       '<div class="col-xs-8 left">'
                    +           '<b class="icon ' + typeArr[1] + ' pull-left"></b>'
                    +           '<dl class="pull-left">'
                    +               '<dt>' + typeArr[0] + '</dt>'
                    +               '<dd>' + tradeList[i].trade_at + '</dd>'
                    +           '</dl>'
                    +       '</div>'
                    +       '<div class="col-xs-4 money_num">' + typeArr[2] + tradeList[i].serve_price + '</div>'
                    +   '</li>';
            }
        }
        setTimeout(function(){
            $('.capital-detail .detail-list').append($(html));
            isLoading(false);
        },500);
    }else{
        setTimeout(function(){
            showMessage(banJson.msg);
            isLoading(false);
            user_config.currPageNo--;
        },500);
    }
}
function isLoading(flag){
    if(flag){
        user_config.canNext = false;
        $('.load-add').addClass('load-adding').text('正在加载...');
    }else{
        user_config.canNext = true;
        $('.load-add').removeClass('load-adding').text('加载更多');
    }
}
function showMessage(message){
    var content = {};
    content.title = '';
    content.text = message;
    content.animation = 'slide-from-bottom';
    content.timer = 1000;
    content.showConfirmButton = false;
    swal(content);
}

var addMore = $('.load-add').hammer();
addMore.on('tap',function(){
    if(user_config.canNext){
        addBanlance(++user_config.currPageNo);
    }
});
var msgRefresh = $('.glyphicon-refresh').hammer();
msgRefresh.on('tap',function(){
    window.location.reload();
});


